home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / mus / edit / AlgoRhythms.lha / AlgoRhythms / Source / Window.c < prev    next >
C/C++ Source or Header  |  1994-11-25  |  7KB  |  218 lines

  1. /* Window.c
  2.     Copyright (c) 1990,1991,1992,1993 by Thomas E. Janzen
  3.     All Rights Reserved
  4.  
  5.     THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
  6.     BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
  7.     CHANGES FOR FREE DISTRIBUTION.  NO TITLE TO AND OWNERSHIP OF THE
  8.     SOFTWARE IS HEREBY TRANSFERRED.  THOMAS E. JANZEN ASSUMES NO 
  9.     RESPONSIBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
  10.     
  11.     Thomas E. Janzen
  12.     208A Olde Derby Road
  13.     Norwood, MA  02062-1761
  14.     (617)769-7733
  15.  
  16. **  FACILITY:
  17. **
  18. **    AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
  19. **    compiled with SAS/C Amiga Compiler 6.50 
  20. **
  21. **  ABSTRACT:
  22. **
  23. **    Window.c Opens and manages the Workbench-type window used by
  24. **    AlgoRhythms.
  25. **
  26. **  AUTHORS: Thomas E. Janzen
  27. **
  28. **  CREATION DATE:    26-MAR-1990
  29. **
  30. **  MODIFICATION HISTORY:
  31. **    DATE    NAME    DESCRIPTION
  32. **    24 SEP 91 T. Janzen use algorhythms.col to specify screen colors
  33. **    8 DEC 91 T. Janzen conform to SAS/C 5.10b remove extern from functs
  34. **    4 Jan 92 TEJ  last changes for 2.0
  35. **    12 Oct 93 TEJ remove unecessary library opens/closes.
  36. **                  Change to use AmigaDOS 2.0 libraries. 
  37. **                  No longer support interlace.
  38. **    30 DEC 93 TEJ Go back to 4 screen colors.
  39. **--
  40. */
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <math.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include <exec/types.h>
  47. #include <exec/nodes.h>
  48. #include <exec/lists.h>
  49. #include <intuition/intuition.h>
  50. #include <intuition/screens.h>
  51. #include <graphics/text.h>
  52. #include <proto/asl.h>
  53. #include <proto/exec.h>
  54. #include <proto/graphics.h>
  55. #include <proto/intuition.h>
  56. #include <proto/dos.h>
  57. #include <proto/gadtools.h>
  58. #include <graphics/display.h>
  59. #include "Window.h"
  60.  
  61. #define TRUE 1
  62. #define FALSE 0
  63.  
  64. /* WINDOW */
  65. struct Screen *main_screen;
  66. struct Window *w;
  67. struct RastPort *rast_port;
  68. struct ViewPort *view_port;
  69. void *vi;
  70.  
  71. struct TextAttr font_choice = {(STRPTR)"topaz.font", TOPAZ_EIGHTY, 0, 0};
  72.  
  73. static void StripIntuiMessages(struct MsgPort *mp, struct Window *win);
  74.  
  75. void open_wind(void) 
  76. /*
  77. ** FUNCTIONAL DESCRIPTION:
  78. **
  79. ** DESIGN:
  80. **  ROUTINE
  81. **  : open color file
  82. **  : IF file opened
  83. **  : : read in intensities for reds, greens, and blues
  84. **  : : close the color file
  85. **  : ENDIF
  86. **  : OpenScreen()
  87. **  : FOR color_index = 0 to 7
  88. **  : : Set the colors
  89. **  : ENDFOR
  90. **  : copy screen pointer to new window
  91. **  : copy rastport from window struct
  92. **  : copy ViewPort from screen
  93. **  ENDROUTINE
  94. */
  95. {
  96.     static char tmp_str[80];
  97.     auto struct ColorSpec colors[] 
  98.         =  {{0, 0x0, 0x9, 0xF}, {1, 0x0, 0x0, 0x6},
  99.             {2, 0xB, 0xF, 0xF}, {3, 0x0, 0xF, 0xF}, {-1,0x0, 0x0, 0x0}};
  100.     auto FILE *color_file;
  101.     static char screen_title[55] = 
  102.         "AlgoRhythms ©1993 Thomas E. Janzen All Rights Reserved";
  103.     auto UWORD pens[] = {0xFFFF};
  104.     color_file = fopen(T_COLOR_FILE, "r");
  105.     if (color_file != NULL)
  106.     {
  107.         fgets(tmp_str, 80, color_file);
  108.         tmp_str[79] = '\0';
  109.         sscanf(tmp_str, "%hd %hd %hd %hd", 
  110.             &(colors[0].Red), &(colors[1].Red), &(colors[2].Red), 
  111.             &(colors[3].Red));
  112.         fgets(tmp_str, 80, color_file);
  113.         tmp_str[79] = '\0';
  114.         sscanf(tmp_str, "%hd %hd %hd %hd", 
  115.             &(colors[0].Green), &(colors[1].Green), &(colors[2].Green), 
  116.             &(colors[3].Green));
  117.         fgets(tmp_str, 80, color_file);
  118.         tmp_str[79] = '\0';
  119.         sscanf(tmp_str, "%hd %hd %hd %hd", 
  120.             &(colors[0].Blue), &(colors[1].Blue), &(colors[2].Blue), 
  121.             &(colors[3].Blue));
  122.         fclose(color_file);
  123.     }
  124.     main_screen = OpenScreenTags(  NULL,
  125.                                 SA_Pens, (ULONG)pens,
  126.                                 SA_DisplayID, HIRES_KEY,
  127.                                 SA_Depth, C_WINDOW_DEPTH,
  128.                                 SA_Title, (ULONG) screen_title,
  129.                                 SA_Height, 200,
  130.                                 SA_Colors, colors,
  131.                                 SA_Font, &font_choice,
  132.                                 TAG_DONE);
  133.     if (NULL == (vi = GetVisualInfo(main_screen, TAG_END)))
  134.     {
  135.         ;
  136.     }
  137.     w = OpenWindowTags(NULL,
  138.                         WA_Left,        0,
  139.                         WA_Top,         10,
  140.                         WA_Width,       640,
  141.                         WA_Height,      190,
  142.                         WA_MinWidth,    400,
  143.                         WA_MinHeight,   190,
  144.                         WA_MaxWidth,    ~0,
  145.                         WA_MaxHeight,   ~0,
  146.                         WA_CloseGadget, TRUE,
  147.                         WA_SizeGadget,  TRUE,
  148.                         WA_DepthGadget, TRUE,
  149.                         WA_DragBar,     TRUE,
  150.                         WA_Activate,    TRUE,
  151.                         WA_NoCareRefresh,TRUE,
  152.                         WA_CustomScreen, main_screen,
  153.                         WA_Title,       "Random Form",
  154.           WA_IDCMP, IDCMP_REQSET | IDCMP_NEWSIZE | IDCMP_CLOSEWINDOW | 
  155.           IDCMP_MENUPICK | IDCMP_GADGETUP,
  156.           WA_Flags, WFLG_SMART_REFRESH | WFLG_GIMMEZEROZERO,
  157.                        TAG_DONE);
  158.     rast_port = w->RPort;            /* Get the raster port pointer */
  159.     view_port = &w->WScreen->ViewPort;    /* Get the view port pointer */
  160.     return;
  161. }
  162.  
  163. void shut_window(void) 
  164. /*
  165. ** FUNCTIONAL DESCRIPTION:
  166. **  Shuts down libraries, windows, and screen.
  167. **
  168. ** DESIGN:
  169. **  ROUTINE
  170. **  : CloseWindow()
  171. **  : CloseScreen()
  172. **  : OpenWorkBench()
  173. **  ENDROUTINE
  174. */
  175. {
  176.     CloseWindowSafely(w);
  177.     CloseScreen(main_screen);
  178.     FreeVisualInfo(vi);
  179.     OpenWorkBench();
  180.     return;
  181. }
  182. /*
  183. ** CloseWindowSafely and StripIntuiMessages are taken from the
  184. ** Amiga ROM Kernel Reference Manual, Includes and Autodocs,
  185. ** for V2, P 314 under the autodocs for CloseWindow() with minor 
  186. ** modifications.
  187. */
  188. void CloseWindowSafely(struct Window *win)
  189. {
  190.     Forbid(); /* prevent Intuition race conditions */
  191.     /* Return to sender - addressee unknown */
  192.     StripIntuiMessages(win->UserPort, win);
  193.     win->UserPort = NULL; /* zero out shared port without hurting others */
  194.     ModifyIDCMP(win, 0L); /* Clear out IDCMP flags to stop events */
  195.     Permit(); /* Done hogging the OS */
  196.     CloseWindow(win);
  197.     
  198.     return;
  199. }
  200.  
  201. static void StripIntuiMessages(struct MsgPort *mp, struct Window *win)
  202. {
  203.     auto struct IntuiMessage *msg;
  204.     auto struct Node *succ;
  205.     
  206.     msg = ( struct IntuiMessage *)mp->mp_MsgList.lh_Head;
  207.     while(succ = msg->ExecMessage.mn_Node.ln_Succ)
  208.     {
  209.         if(msg->IDCMPWindow == win)
  210.         {
  211.              Remove((struct Node *)msg);
  212.              ReplyMsg((struct Message *)msg);
  213.         }
  214.         msg = (struct IntuiMessage *) succ;
  215.     }
  216.     return;
  217. }
  218.